package org.chnxi.study.springcloud.dubbo.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PerformanceDescrib {

    /**
     * 发送请求的时间
     */
    private long requestTimestamp;

    /**
     * 发送响应的时间
     */
    private long responseTimestamp;

    /**
     * 处理耗时
     */
    private long spend;

    public static PerformanceDescrib newInstance4Request(){
        PerformanceDescrib pd = new PerformanceDescrib();
        pd.setRequestTimestamp(System.currentTimeMillis());
        return pd;
    }

    public static PerformanceDescrib newInstance4Response(PerformanceDescrib reqPerformanceDesc){
        PerformanceDescrib pd = new PerformanceDescrib();
        long curTimestamp = System.currentTimeMillis();
        if(reqPerformanceDesc == null){
            pd.setRequestTimestamp(-1);
            pd.setSpend(-1);
            pd.setResponseTimestamp(curTimestamp);
        }else{
            pd.setRequestTimestamp(reqPerformanceDesc.getRequestTimestamp());
            pd.setResponseTimestamp(curTimestamp);
            pd.setSpend(pd.getResponseTimestamp() - pd.getRequestTimestamp());
        }
        return pd;
    }
}
